﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OverlayWindow.xaml.cs" company="arvystate.net">
//   arvystate.net
// </copyright>
// <summary>
//   Interaction logic for NotificationMainControl.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace PresentationAid.Control
{
    using System;
    using System.Windows;
    using System.Windows.Media.Imaging;
    using System.Windows.Threading;

    using NLog;

    /// <summary>
    /// Interaction logic for NotificationMainControl
    /// </summary>
    public partial class OverlayWindow
    {
        #region Static Fields

        /// <summary>
        /// The logger.
        /// </summary>
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        #endregion

        #region Fields

        /// <summary>
        /// The _dispatcher timer.
        /// </summary>
        private readonly DispatcherTimer dispatcherTimer;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="OverlayWindow"/> class.
        /// </summary>
        public OverlayWindow()
        {
            Logger.Trace("Init: OverlayWindow - CloseInterval: {0}", 4000);

            this.InitializeComponent();

            this.dispatcherTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(4000) };

            this.dispatcherTimer.Tick += this.DispatcherTimerTick;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the close delay.
        /// </summary>
        public int CloseDelay
        {
            get
            {
                return (int)Math.Round(this.dispatcherTimer.Interval.TotalMilliseconds);
            }

            set
            {
                this.dispatcherTimer.Interval = TimeSpan.FromMilliseconds(value);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the window has no delay, if this is set to true, the window will not hide on delay, it must be done manually in code
        /// with hiding the window.
        /// </summary>
        public bool HasNoDelay { get; set; }

        /// <summary>
        /// Gets or sets the overlay image.
        /// </summary>
        public string OverlayImage
        {
            get
            {
                if (this.OverlayImageElement.Visibility == Visibility.Collapsed)
                {
                    return string.Empty;
                }

                BitmapImage image = (BitmapImage)this.OverlayImageElement.Source;

                return image.UriSource.ToString();
            }

            set
            {
                if (value == null)
                {
                    this.OverlayImageElement.Visibility = Visibility.Collapsed;
                }
                else
                {
                    Uri uriSource = new Uri("/PresentationAid;component/Resources/" + value, UriKind.RelativeOrAbsolute);
                    this.OverlayImageElement.Source = new BitmapImage(uriSource);
                }
            }
        }

        /// <summary>
        /// Gets or sets the overlay text.
        /// </summary>
        public string OverlayText
        {
            get
            {
                return this.OverlayTextTextBlock.Text;
            }

            set
            {
                this.OverlayTextTextBlock.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the overlay title.
        /// </summary>
        public string OverlayTitle
        {
            get
            {
                return this.OverlayTitleTextBlock.Text;
            }

            set
            {
                this.OverlayTitleTextBlock.Text = value.ToUpper();
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The dispatcher timer tick.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void DispatcherTimerTick(object sender, EventArgs e)
        {
            Logger.Debug("Event: TimerTick - CloseWindow, IsVisible: {0}", this.IsVisible);

            this.dispatcherTimer.Stop();

            if (this.IsVisible)
            {
                this.Hide();
            }
        }

        /// <summary>
        /// The window activated.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void WindowActivated(object sender, EventArgs e)
        {
            Logger.Debug("Event: WindowActivated - HasNoDelay: {0}", this.HasNoDelay);

            if (!this.HasNoDelay)
            {
                this.dispatcherTimer.Start();
            }
        }

        #endregion
    }
}